package servlet;
import model.Goods;
import org.apache.commons.fileupload2.core.DiskFileItemFactory;
import org.apache.commons.fileupload2.jakarta.JakartaServletFileUpload;
import org.apache.commons.fileupload2.core.FileItem;
import org.apache.commons.fileupload2.core.FileUploadException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import service.GoodsService;
import java.io.*;
import java.nio.charset.Charset;
import java.util.List;
@WebServlet(name = "goods_add", urlPatterns = "/goods_add")
public class AdminGoodsAddServlet extends HttpServlet {
    private GoodsService gService = new GoodsService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.sendRedirect("huangshan.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        File f = new File("E://TempFolder");
        if (!f.exists()){
            f.mkdir();
        }
        DiskFileItemFactory factory=DiskFileItemFactory.builder().get();
        JakartaServletFileUpload upload =new JakartaServletFileUpload(factory);
        upload.setHeaderCharset(Charset.defaultCharset());
        Charset charset = Charset.forName("UTF-8");
        try{
            List<FileItem> list=upload.parseRequest(request);
            Goods g =new Goods();
            for (FileItem item:list){
                if (item.isFormField()){
                    switch (item.getFieldName()){
                        case "name":
                            g.setName(item.getString(charset));
                            break;
                        case "price":
                            g.setPrice(Integer.parseInt(item.getString(charset)));
                            break;
                        case "intro":
                            g.setIntro(item.getString(charset));
                            break;
                        case "stock":
                            g.setStock(Integer.parseInt(item.getString(charset)));
                            break;
                        case "typeid":
                            g.setTypeid(Integer.parseInt(item.getString(charset)));
                            break;
                    }
                }else {
                    if (item.getInputStream().available()<=0)continue;
                    String fileName = item.getName();
                    System.out.println("fileName:"+fileName);
                    fileName ="/"+fileName;
                    System.out.println("fileName:"+fileName);
                    String path =this.getServletContext().getRealPath("/images")+fileName;
                    InputStream in =item.getInputStream();
                    FileOutputStream out =new FileOutputStream(path);
                    byte[] buffer=new byte[1024];
                    int len =0;
                    while ((len=in.read(buffer))>0){
                        out.write(buffer);
                    }
                    in.close();
                    out.close();
                    item.delete();
                    switch (item.getFieldName()){
                        case "cover":
                            g.setCover("/images"+fileName);
                            break;

                    }
                }
            }
            gService.insert(g);
            response.sendRedirect("userlistAdmin.jsp");
        }catch (FileUploadException e){
            e.printStackTrace();
        }
    }
}
